package com.campusexpresspickup.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.campusexpresspickup.entity.UserMenu;
import com.github.pagehelper.PageHelper;
import com.campusexpresspickup.entity.OauthRole;
import com.campusexpresspickup.entity.UserResource;
import com.campusexpresspickup.entity.UserRoleMenuRelation;
import com.campusexpresspickup.entity.UserRoleResourceRelation;
import com.campusexpresspickup.mapper.OauthRoleMapper;
import com.campusexpresspickup.mapper.UserMenuMapper;
import com.campusexpresspickup.mapper.UserResourceMapper;
import com.campusexpresspickup.mapper.UserRoleMenuRelationMapper;
import com.campusexpresspickup.mapper.UserRoleResourceRelationMapper;
import com.campusexpresspickup.service.IOauthRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lq
 * @since 2021-11-15
 */
@Service
public class OauthRoleServiceImpl extends ServiceImpl<OauthRoleMapper, OauthRole> implements
    IOauthRoleService {

  private final OauthRoleMapper oauthRoleMapper;
  private final UserResourceMapper userResourceMapper;
  private final UserMenuMapper userMenuMapper;
  private final UserRoleMenuRelationMapper userRoleMenuRelationMapper;
  private final UserRoleResourceRelationMapper userRoleResourceRelationMapper;

  public OauthRoleServiceImpl(OauthRoleMapper oauthRoleMapper,
      UserResourceMapper userResourceMapper, UserMenuMapper userMenuMapper,
      UserRoleMenuRelationMapper userRoleMenuRelationMapper,
      UserRoleResourceRelationMapper userRoleResourceRelationMapper) {
    this.oauthRoleMapper = oauthRoleMapper;
    this.userResourceMapper = userResourceMapper;
    this.userMenuMapper = userMenuMapper;
    this.userRoleMenuRelationMapper = userRoleMenuRelationMapper;
    this.userRoleResourceRelationMapper = userRoleResourceRelationMapper;
  }

  @Override
  public List<OauthRole> list(String keyword, Integer pageSize, Integer pageNum) {
    PageHelper.startPage(pageNum, pageSize);
    return oauthRoleMapper.findRoleList(keyword);
  }

  @Override
  public List<UserMenu> getMenuList(Integer userId) {
    return userMenuMapper.getMenuList(userId);
  }

  @Override
  public List<UserMenu> listMenu(Integer roleId) {
    return userMenuMapper.getMenuListByRoleId(roleId);
  }

  @Override
  public List<UserResource> listResource(Integer roleId) {
    return userResourceMapper.getResourceListByRoleId(roleId);
  }

  @Override
  public int allocMenu(Integer roleId, List<Integer> menuIds) {
    Wrapper<UserRoleMenuRelation> queryWrapper = new QueryWrapper<>();
    queryWrapper.getEntity().setRoleId(roleId);
    userRoleMenuRelationMapper.delete(queryWrapper);
    List<UserRoleMenuRelation> menuRelationList = new ArrayList<>();
    for (Integer id : menuIds) {
      UserRoleMenuRelation menuRelation = new UserRoleMenuRelation();
      menuRelation.setMenuId(id);
      menuRelation.setRoleId(roleId);
      menuRelationList.add(menuRelation);
    }
    userRoleMenuRelationMapper.insertRoleMenuList(menuRelationList);
    menuRelationList.clear();
    return menuIds.size();
  }

  @Override
  public int allocResource(Integer roleId, List<Integer> resourceIds) {
    Wrapper<UserRoleResourceRelation> queryWrapper = new QueryWrapper<>();
    queryWrapper.getEntity().setRoleId(roleId);
    userRoleResourceRelationMapper.delete(queryWrapper);
    List<UserRoleResourceRelation> list = new ArrayList<>();
    for (Integer id : resourceIds) {
      UserRoleResourceRelation resourceRelation = new UserRoleResourceRelation();
      resourceRelation.setRoleId(roleId);
      resourceRelation.setResourceId(id);
      list.add(resourceRelation);
    }
    userRoleResourceRelationMapper.insertRoleResourceList(list);
    list.clear();
    return resourceIds.size();
  }
}
